package view;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MouseInfo;
import java.awt.Point;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.swing.BoxLayout;
import javax.swing.JPanel;

import model.Card;
import model.CardStack;
import model.GameModel;
import model.Position;
import controller.MetaDeckController;

@SuppressWarnings("serial")
public class GameComponent extends JPanel{
	final GameModel model;
	final TableComponent table;
	final Map<Position,HandComponent> hands;
	public GameComponent(GameModel model) {
		super();
		this.model = model;
		setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
		
		table = new TableComponent(model.getTable());
		final JPanel eastPanel = new TableEastPanel();
		final JPanel westPanel = new TableWestPanel();
		final JPanel northPanel = new TableNorthPanel();
		final JPanel southPanel = new TableSouthPanel();
		
		hands = new HashMap<Position, HandComponent>();
		hands.put(Position.NORTH, new HandComponent(model.getPlayer(Position.NORTH)));
		hands.put(Position.SOUTH, new HandComponent(model.getPlayer(Position.SOUTH)));
		northPanel.add(hands.get(Position.NORTH));
		//northPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
		southPanel.add(hands.get(Position.SOUTH));
		//southPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
		
		
		final JPanel centerRowPanel = new JPanel();
		centerRowPanel.add(westPanel);
		centerRowPanel.add(table);
		centerRowPanel.add(eastPanel);
		add(hands.get(Position.NORTH));
		add(centerRowPanel);
		add(hands.get(Position.SOUTH));
		
		this.setBackground(Color.DARK_GRAY);
		centerRowPanel.setBackground(Color.DARK_GRAY);
	}
	
	public TableComponent getTable() {
		return table;
	}
	public HandComponent getHand(Position pos) {
		return hands.get(pos); 
	}
	
	
	public void paint(Graphics tg) {
		super.paint(tg);
		Graphics2D g = (Graphics2D)tg;
		
		Point tp = MouseInfo.getPointerInfo().getLocation();
		/** The position of the mouse cursor within the game component. */
		Point currentMouseLocation = new Point(tp.x-getLocationOnScreen().x, tp.y-getLocationOnScreen().y);
		
		//Draw the cards being dragged if necessary.
		if(table.getDragging()) {
			int max = table.getSelection().totalSize();
			g.setColor(Color.RED);
			int xpos = currentMouseLocation.x;
			int ypos = currentMouseLocation.y;
			for(int n=0; n<max; n++) {
				g.drawRect(xpos-ImageLoader.IMAGE_WIDTH/2-2, ypos-ImageLoader.IMAGE_HEIGHT/2-2, 
						ImageLoader.IMAGE_WIDTH+3, ImageLoader.IMAGE_HEIGHT+3);
				if(n==max-1) {
					CardStack stack = table.getSelection().get(0).stack;
					Card card = stack.getCards().get(stack.size()-1);
					Image im = (stack.getFaceUp())?ImageLoader.loadImage(card):ImageLoader.loadImage("back");
					g.drawImage(im, 
							xpos-ImageLoader.IMAGE_WIDTH/2, 
							ypos-ImageLoader.IMAGE_HEIGHT/2, null);
				}
				xpos+=1;
				ypos+=1;
			}
			//Draw stack size TODO make prettier
			g.setColor(Color.CYAN);
			if(table.getSelection().totalSize()>1)
				g.drawString(table.getSelection().totalSize()+"", 
					currentMouseLocation.x-2, currentMouseLocation.y-ImageLoader.IMAGE_HEIGHT/2+2);
		}
		if(MetaDeckController.getInstance().getGame().isHandDragging()) {
			Set<Card> stack = hands.get(MetaDeckController.getInstance().getMyPosition()).getSelection();
			
			
			int max = stack.size();
			g.setColor(Color.RED);
			int xpos = currentMouseLocation.x;
			int ypos = currentMouseLocation.y;
			Iterator<Card> it = stack.iterator();
			for(int n=0; n<max; n++) {
				Card card = it.next();
				g.drawRect(xpos-ImageLoader.IMAGE_WIDTH/2-2, ypos-ImageLoader.IMAGE_HEIGHT/2-2, 
						ImageLoader.IMAGE_WIDTH+3, ImageLoader.IMAGE_HEIGHT+3);
				if(n==max-1) {
					g.drawImage(ImageLoader.loadImage(card), 
							xpos-ImageLoader.IMAGE_WIDTH/2, 
							ypos-ImageLoader.IMAGE_HEIGHT/2, null);
				}
				xpos+=1;
				ypos+=1;
			}
			//Draw stack size TODO make prettier
			g.setColor(Color.CYAN);
			if(stack.size()>1)
			g.drawString(stack.size()+"", 
					currentMouseLocation.x-2, currentMouseLocation.y-ImageLoader.IMAGE_HEIGHT/2+2);
		}
	}
}
